package com.xialuo.study.tools;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索工具，用于模拟搜索功能
 */
@Component
@Slf4j
public class SearchTool {

    private final WebClient webClient;
    private final String apiKey;

    public SearchTool(@Value("${tavily.api.key}") String apiKey) {
        this.apiKey = apiKey;
        this.webClient = WebClient.builder()
                .baseUrl("https://api.tavily.com")
                .build();
    }

    @Tool(description = "搜索互联网上的信息。提供查询字符串，返回相关搜索结果")
    Response searchWeb(@ToolParam(description = "要进行网络搜索的内容") Request request) {
        log.info("执行Tavily搜索查询: {}", request.query());

        TavilyRequest tavilyRequest = new TavilyRequest(request.query());

        TavilyResponse tavilyResponse = webClient.post()
                                                 .uri("/search")
                                                 .header("Content-Type", "application/json")
                                                 .header("Authorization", "Bearer " + apiKey)
                                                 .bodyValue(tavilyRequest)
                                                 .retrieve()
                                                 .bodyToMono(TavilyResponse.class)
                                                 .block();

        List<SearchResult> results = new ArrayList<>();
        if (tavilyResponse != null && tavilyResponse.results != null) {
            for (TavilyResult result : tavilyResponse.results) {
                results.add(new SearchResult(
                        result.title,
                        result.content,
                        result.url
                ));
            }
        }
        return new Response(results);
    }


    /**
     * 搜索请求对象
     *
     * @param query 搜索查询字符串
     */
    public record Request(String query) {
    }

    /**
     * 搜索响应对象
     *
     * @param results 搜索结果列表
     */
    public record Response(List<SearchResult> results) {
    }

    /**
     * 搜索结果对象
     *
     * @param title 结果标题
     * @param content 结果内容
     * @param url 结果URL
     */
    public record SearchResult(String title, String content, String url) {
    }

    // 内部类用于Tavily API请求
    private record TavilyRequest(
        String query
    ) {}

    // 内部类用于Tavily API响应
    private static class TavilyResponse {
        @JsonProperty("results")
        private List<TavilyResult> results;
    }

    private static class TavilyResult {
        @JsonProperty("title")
        private String title;

        @JsonProperty("content")
        private String content;

        @JsonProperty("url")
        private String url;
    }
}
